# -*- coding: utf-8 -*-
"""
Created on 15-12-23 下午2:20
@file: mongodb.py
@author: Liangrong Li
@contact: liliangrong0@126.com
"""

from pymongo import MongoClient
import datetime

# client = MongoClient()      # 默认
client = MongoClient('localhost',27017)      # 使用MongoClient创建连接
# client = MongoClient('mongodb://localhost:27017/')      # 第三种方式

db = client.test    # 链接test数据库
# db = client['test']    # 链接test数据库, 第二种形式

collection = db.posts         # 链接test数据库中的post集合，相当于mysql中的表
# collection = db['posts']         # 第二种方式

post1 = {"title":"I Love Python",
         "slug":"i-love-python",
         "author":"SErHo",
         "content":"I Love Python....",
         "tags":["Love","Python"],
         "time":datetime.datetime.now()}

post2 = {"title":"Python and MongoDB",
         "slug":"python-mongodb",
         "author":"SErHo",
         "content":"Python and MongoDB....",
         "tags":["Python","MongoDB"],
         "time":datetime.datetime.now()}

post3 = {"title":"SErHo Blog",
         "slug":"serho-blog",
         "author":"Akio",
         "content":"SErHo Blog is OK....",
         "tags":["SErHo","Blog"],
         "time":datetime.datetime.now()}

post_id = collection.insert_one(post1).inserted_id
print post_id

print collection.find_one()